import { login, logout, qqLogin, oauth2LoginCallback, getUserInfo } from '@/api/index'
import { getToken, setToken, removeToken, getAvatar, setAvatar, getName, setName, removeName, removeAvatar,getPermission,setPermission,removePermission } from '@/utils/auth'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: getName(),
    avatar: getAvatar(),
    permission: getPermission()
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_PERMISSION: (state, permission) => {
    state.permission = permission
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      login(userInfo).then(res => {
        let data = res.data
        commit('SET_TOKEN', data.token)
        commit('SET_NAME', data.nickname)
        commit('SET_AVATAR', data.avatar)
        commit('SET_PERMISSION', data.permission)
        setToken(data.token)
        setAvatar(data.avatar)
        setName(data.nickname)
        setPermission(data.permission)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  getUserInfo({ commit }) {
    return new Promise((resolve, reject) => {
      getUserInfo().then(res => {
        let data = res.data
        if (data && typeof data.nickname != "undefined" && typeof data.avatar != "undefined") {
          commit('SET_NAME', data.nickname)
          commit('SET_AVATAR', data.avatar)
          commit('SET_PERMISSION', data.permission)
          setPermission(data.permission)
          setAvatar(data.avatar)
          setName(data.nickname)
        }
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        removeName() 
        removeAvatar() 
        removePermission()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  //QQ互联的client-side模式,
  qqlogin({ commit }, accessToken) {
    console.log("qqlogin---accessToken:" + accessToken);
    return new Promise((resolve, reject) => {
      qqLogin(accessToken).then(res => {
        let data = res.data
        commit('SET_TOKEN', data.token)
        commit('SET_NAME', data.nickname)
        commit('SET_AVATAR', data.avatar)
        commit('SET_PERMISSION', data.permission)
        setPermission(data.permission)
        setToken(data.token)
        setAvatar(data.avatar)
        setName(data.nickname)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  //QQ互联的server-side模式,
  authCodelogin({ commit }, authcallback) {
    console.log("qqCodelogin----code:" + authcallback.code);
    return new Promise((resolve, reject) => {
      oauth2LoginCallback(authcallback.platform,authcallback).then(res => {
        let data = res.data
        console.log("server-side模式,  data==" + JSON.stringify(data));
        console.log("server-side模式,  data==" + data.token);
        commit('SET_TOKEN', data.token)
        commit('SET_NAME', data.nickname)
        commit('SET_AVATAR', data.avatar)
        commit('SET_PERMISSION', data.permission)
        setPermission(data.permission)
        setToken(data.token)
        setAvatar(data.avatar)
        setName(data.nickname)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  //获取token  vuex 一刷新就没了,所以要存到cookie里边
  getToken() {
    return new Promise((resolve) => {
      if (state.token) {
        resolve(state.token)
      } else {
        resolve()
      }
    })
  },
  getAvatar() {
    // const { username, password,captcha,state } = userInfo
    return new Promise((resolve) => {
      if (state.avatar) {
        resolve(state.avatar)
      } else {
        resolve()
      }
    })
  },
  getName() {
    return new Promise((resolve) => {
      if (state.name) {
        resolve(state.name)
      } else {
        resolve()
      }
    })
  },

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

